package com.auctionhaus

class ListingController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def addBid = {
        def listingInstance = Listing.findByListName(params.listName)
        listingInstance.addToBids(params.bid)
    }

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        println params
        params.max = Math.min(params.max ? params.int('max') : 5, 100)
        params.offset = (params.offset ? params.int('offset') : 0);
        if (!params.sort) {
            params.sort = "creationDate"
            params.order = "desc"
        }
        def c = Listing.createCriteria()
        def results = c.list(max: params.max, offset: params.offset) {
            ge("endDateTime", new Date())
            order(params.sort, params.order)
        }
        [listingInstanceList:results, listingInstanceTotal: results.totalCount]

    }

    def create = {
        def listingInstance = new Listing()
        listingInstance.endDateTime = new Date() + 1;
        listingInstance.properties = params
        return [listingInstance: listingInstance]
    }

    def save = {
        params.creationDate = new Date()
        params.seller = Customer.findByEmailAddressIlike(params.seller.emailAddress)
        def listingInstance = new Listing(params)

        if (listingInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'listing.label', default: 'Listing'), listingInstance.id])}"
            flash.message = "Listing created: " + listingInstance.listName
            redirect(action: "show", id: listingInstance.id)
        }
        else {
            render(view: "create", model: [listingInstance: listingInstance])
        }
    }

    def show = {
        def listingInstance = Listing.get(params.id)
        def bidInstance = new Bid()
        bidInstance.properties = params
        if (!listingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
            redirect(action: "list")
        }
        else {
            [listingInstance: listingInstance, myBidEntity: bidInstance]
        }
    }

    def edit = {
        def listingInstance = Listing.get(params.id)
        if (!listingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [listingInstance: listingInstance]
        }
    }

    def update = {
        def listingInstance = Listing.get(params.id)
        if (listingInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (listingInstance.version > version) {
                    
                    listingInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'listing.label', default: 'Listing')] as Object[], "Another user has updated this Listing while you were editing")
                    render(view: "edit", model: [listingInstance: listingInstance])
                    return
                }
            }
            listingInstance.properties = params
            if (!listingInstance.hasErrors() && listingInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'listing.label', default: 'Listing'), listingInstance.id])}"
                redirect(action: "show", id: listingInstance.id)
            }
            else {
                render(view: "edit", model: [listingInstance: listingInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def listingInstance = Listing.get(params.id)
        if (listingInstance) {
            try {
                listingInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'listing.label', default: 'Listing'), params.id])}"
            redirect(action: "list")
        }
    }
}
